package com.lg.okframe.base;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.lg.okframe.R;
import com.zhy.autolayout.AutoLayoutActivity;

import cn.bingoogolapple.swipebacklayout.BGASwipeBackHelper;


/**
 * Created by 刘国 on 2018/7/30.
 * ac基类
 */

public abstract class BaseActivity extends AppCompatActivity implements BGASwipeBackHelper.Delegate,View.OnClickListener{

    private LinearLayout linearLayout;
    private View title_bar;
    private BGASwipeBackHelper mSwipeBackHelper;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        title_bar = View.inflate(this, R.layout.title_bar, null);
        View child_view  = View.inflate(this, setMainView(), null);

        linearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.addView(title_bar);
        linearLayout.addView(child_view,layoutParams);

        //去除标题栏
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        initView();
        initData();
        initSwipeBackFinish();
    }

    /**
     * 打开新的ac
     * @param tosActivity
     */
   public void  ToStartActivity(Class tosActivity){
       Intent intent = new Intent(this, tosActivity);
        startActivity(intent);

   }


    /**
     * 显示toast
     * @param mess
     * @param time
     */
   public void  ToastShow(String mess,int time){
       if(time==1){
           Toast.makeText(this, ""+mess, Toast.LENGTH_SHORT).show();
       }else{
           Toast.makeText(this, ""+mess, Toast.LENGTH_LONG).show();
       }
   }



    /**
     *
     * @param left_resID 左边按钮图标
     * @param right_resID 右边按钮图标
     * @param text  中间文字
     * @param hasBootomTab 是否有下部tab
     */
    public LinearLayout initTitlebar(int left_resID,int right_resID,String text,boolean hasBootomTab){
        ImageView base_left = (ImageView)title_bar. findViewById(R.id.base_left);
        ImageView base_right = (ImageView)title_bar. findViewById(R.id.base_right);
        TextView base_title = (TextView)title_bar. findViewById(R.id.base_title);
        if(left_resID!=0){
            base_left.setImageResource(left_resID);
        }else{
            base_left.setVisibility(View.GONE);
        }

        if(right_resID!=0){
            base_right.setImageResource(right_resID);
        }else{
            base_right.setVisibility(View.GONE);
        }

        if(!text.isEmpty())
        base_title.setText(text);
        base_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                leftIconClickListener(view);
            }
        });
        base_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rightIconClickListener(view);
            }
        });
        if(!hasBootomTab)
        setContentView(linearLayout);

        return linearLayout;
    }

    /**
     * 是否支持滑动返回。这里在父类中默认返回 true 来支持滑动返回，如果某个界面不想支持滑动返回则重写该方法返回 false 即可
     *
     * @return
     */
    @Override
    public boolean isSupportSwipeBack() {
        return true;
    }

    /**
     * 滑动返回执行完毕，销毁当前 Activity
     */
    @Override
    public void onSwipeBackLayoutExecuted() {
        mSwipeBackHelper.swipeBackward();
    }




    @Override
    public void onBackPressed() {
        // 正在滑动返回的时候取消返回按钮事件
        if (mSwipeBackHelper.isSliding()) {
            return;
        }

        mSwipeBackHelper.backward();
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
         * 初始化滑动返回。在 super.onCreate(savedInstanceState) 之前调用该方法
         */
    private void initSwipeBackFinish() {
        mSwipeBackHelper = new BGASwipeBackHelper(this, this);

        // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackManager.getInstance().init(this) 来初始化滑动返回」
        // 下面几项可以不配置，这里只是为了讲述接口用法。

        // 设置滑动返回是否可用。默认值为 true
        mSwipeBackHelper.setSwipeBackEnable(true);
        // 设置是否仅仅跟踪左侧边缘的滑动返回。默认值为 true
        mSwipeBackHelper.setIsOnlyTrackingLeftEdge(true);
        // 设置是否是微信滑动返回样式。默认值为 true
        mSwipeBackHelper.setIsWeChatStyle(true);
        // 设置阴影资源 id。默认值为 R.drawable.bga_sbl_shadow
        mSwipeBackHelper.setShadowResId(R.drawable.bga_sbl_shadow);
        // 设置是否显示滑动返回的阴影效果。默认值为 true
        mSwipeBackHelper.setIsNeedShowShadow(true);
        // 设置阴影区域的透明度是否根据滑动的距离渐变。默认值为 true
        mSwipeBackHelper.setIsShadowAlphaGradient(true);
        // 设置触发释放后自动滑动返回的阈值，默认值为 0.3f
        mSwipeBackHelper.setSwipeBackThreshold(0.3f);
    }




    protected abstract int setMainView();
    protected  abstract void initView();
    protected abstract void initData();
    protected abstract boolean leftIconClickListener(View view);
    protected abstract boolean rightIconClickListener(View view);


}
